<?php

namespace App\Http\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Http\Requests\ContentSlideRequest;
use App\Models\ContentSlide;
use Illuminate\Http\Request;

class SlideController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
      $slides = ContentSlide::latest()->paginate(5);
      return  view('content.slide_index',compact('slides'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('content.slide_create');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ContentSlideRequest $request , ContentSlide $slide)
    {
        $data = $request->all();
        $data['image'] = str_replace('\\','/',$data['image']);
        $slide->create($data);
        return redirect(route('content.slide.index'))->with('success','幻灯片上传成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\ContentSlide  $contentSlide
     * @return \Illuminate\Http\Response
     */
    public function show(ContentSlide $contentSlide)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\ContentSlide  $contentSlide
     * @return \Illuminate\Http\Response
     */
    public function edit(ContentSlide $slide)
    {
       return view('content.slide_edit',compact('slide'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\ContentSlide  $contentSlide
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ContentSlide $slide)
    {
//        调用模型的update方法将request验证过的数据更新到相应数据表中
        $slide->update($request->all());
        return redirect(route('content.slide.index'))->with('success','幻灯片编辑成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\ContentSlide  $contentSlide
     * @return \Illuminate\Http\Response
     */
    public function destroy(ContentSlide $slide)
    {
//        要删除模型，只要使用实例调用 delete 方法
//        $slide会接收到要删除的数据
        $slide->delete();
        return redirect(route('content.slide.index'))->with('success','删除成功');

    }
}
